<template>
    <div class="container person-list-container">
        <div class="operation-wrapper">
            <el-button
                type="primary"
                size="small"
                @click="handleRouteToDetail()"
                :disabled="!authResult.auth_add">新增人员</el-button>
            <el-button
                size="small"
                :disabled="!authResult.auth_export"
                @click="handleExport">导出</el-button>
        </div>
        <div class="content-wrapper">
            <el-row
                class="search-wrapper"
                :gutter="20">
                <el-form
                    label-position="left"
                    label-width="70px"
                    size="small">
                    <el-col
                        class="filter-item"
                        :span="6">
                        <el-form-item label="省市：">
                            <el-cascader
                                v-model="filterData.region"
                                placeholder="请选择或输入搜索"
                                :options="region"
                                :props='{value:"id", label:"name"}'
                                expand-trigger="hover"
                                filterable
                                change-on-select />
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :span="6">
                        <el-form-item label="人员职业：">
                            <el-autocomplete
                                class="inline-input"
                                v-model="filterData.job"
                                :fetch-suggestions="getJobSuggestions"
                                placeholder="请选择或输入" />
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :span="6">
                        <el-form-item label="性别：">
                            <el-select
                                v-model="filterData.sex"
                                placeholder="请选择">
                                <el-option
                                    label="不限"
                                    value="" />
                                <el-option
                                    label="男"
                                    value="男" />
                                <el-option
                                    label="女"
                                    value="女" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :span="6">
                        <el-form-item label="人员职能：">
                            <el-autocomplete
                                class="inline-input"
                                v-model="filterData.func"
                                :fetch-suggestions="getFuncSuggestions"
                                placeholder="请选择或输入" />
                        </el-form-item>
                    </el-col>
                    <el-col class="search-btn-wrapper">
                        <el-button
                            size="small"
                            type="primary"
                            @click="getTableData">搜索</el-button>
                    </el-col>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    class="stu-table"
                    :data="tableData"
                    height="100%"
                    style="width: 100%;">
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        align="center"
                        label="序号">
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        prop="person_name"
                        :formatter="emptyFormatter"
                        :show-overflow-tooltip="true"
                        label="姓名"/>
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_sex"
                        width="50"
                        label="性别" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_age"
                        width="50"
                        label="年龄" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        show-overflow-tooltip
                        prop="person_job"
                        label="职业" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_function"
                        show-overflow-tooltip
                        label="职能" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_phone"
                        min-width="106"
                        label="电话" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_qq"
                        min-width="105"
                        show-overflow-tooltip
                        label="QQ" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_weixin"
                        min-width="105"
                        show-overflow-tooltip
                        label="微信" />
                    <!-- <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_photos"
                        label="照片" /> -->
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_fee"
                        label="费用" />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="person_remark"
                        show-overflow-tooltip
                        label="备注" />
                    <el-table-column
                        prop="date"
                        label="操作"
                        fixed="right"
                        width="150">
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    :disabled="scope.row.person_photos == '' || scope.row.person_photos == '[]'"
                                    type="text"
                                    @click="handlePreviewPic(scope.row)">查看</el-button>
                                <el-button
                                    type="text"
                                    :disabled="!authResult.auth_edit"
                                    @click="handleRouteToDetail(scope.row.id)">编辑</el-button>
                                <el-button
                                    class="btn-delete"
                                    type="text"
                                    :disabled="!authResult.auth_delete"
                                    @click="handleDelete(scope.row)">删除</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="prev, pager, next"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData" />
            </div>
        </div>
        <pic-preview
            v-model="dialogPicPreviewVisible"
            :pics="picPreviewData.person_photos">
            <div class="pic-preview-desc">
                <p class="title">
                    <span class="main-title">{{ picPreviewData.person_name }}</span>
                    <span class="sub-title">{{ picPreviewData.person_function }}</span>
                </p>
                <p class="remark">{{ picPreviewData.person_remark }}</p>
            </div>
        </pic-preview>
    </div>
</template>

<script>
import Region from '@/assets/js/regin.min';
import picPreview from '@/components/common/pic-preview';

export default {
    components: {
        picPreview
    },
    data() {
        return {
            authResult: {},
            region: Region,
            filterData: {
                region: [],
                job: '',
                func: '',
                sex: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            dialogPicPreviewVisible: false,
            picPreviewData: {}
        };
    },
    watch: {
        dialogPicPreviewVisible(val) {
            if (!val) {
                this.picPreviewData = Object.assign({});
            }
        }
    },
    created() {
        this.$store.commit('SET_HEADER', [
            {
                title: "校园工作室管理",
            },
            {
                title: "人员管理"
            }
        ]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData(page) {
            let pid = this.filterData.region[0] || '',
                cid = this.filterData.region[1] || '';
            let { job, func, sex } = this.filterData;

            // console.log({
            //     page: page || this.page.now_page,
            //     pid,
            //     cid,
            //     job,
            //     function: func,
            //     sex
            // });
            this.$post('/ht/person%5Cget_ist', {
                page: page || this.page.now_page,
                pid,
                cid,
                job,
                function: func,
                sex
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },
        /**
         * 职业输入建议
         */
        getJobSuggestions(query, cb) {
            let arr = [
                { value: '不限' },
                { value: '学生' },
                { value: '主持人' },
                { value: '表演者' },
                { value: '其它' }
            ];
            let results = query ? arr.filter(this.resolveFilter(query)) : arr;

            cb(results);
        },

        /**
         * 职能输入建议
         */
        getFuncSuggestions(query, cb) {
            let arr = [
                { value: '主持人' },
                { value: '工作人员' },
                { value: '现场督导' },
                { value: '歌手' },
                { value: '舞蹈/coser/礼仪' },
                { value: '专业观众' }
            ];
            let results = query ? arr.filter(this.resolveFilter(query)) : arr;

            cb(results);
        },

        /**
         * 输入建议筛选
         */
        resolveFilter(query) {
            return (restaurant) => {
                return (restaurant.value.toLowerCase().indexOf(query.toLowerCase()) === 0);
            };
        },

        /**
         * 导出筛选结果
         */
        handleExport() {
            let pid = this.filterData.region[0] || '',
                cid = this.filterData.region[1] || '';
            let { job, func, sex } = this.filterData;
            let ticket = localStorage.getItem('sessionTicket');
            let base = this.$baseUrl;

            window.open(`${base}/ht/person%5Cexport?pid=${pid}&cid=${cid}&job=${escape(job)}&function=${escape(func)}&sex=${escape(sex)}&__ticket=${ticket}`);
        },

        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        },

        /**
         * 预览图片
         */
        handlePreviewPic(data) {
            this.picPreviewData = Object.assign({}, data);
            this.picPreviewData.person_photos = JSON.parse(this.picPreviewData.person_photos);
            this.dialogPicPreviewVisible = true;
        },

        /**
         * 跳转到编辑页面
         */
        handleRouteToDetail(id) {
            if (id) {
                this.$router.push({
                    name: 'PersonEdit',
                    query: {
                        id: id
                    }
                });
            } else {
                this.$router.push({
                    name: 'PersonEdit'
                });
            }
        },

        /**
         * 删除
         */
        handleDelete(data) {
            this.$confirm('确认删除该人员信息?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/ht/person%5Cdelete', {
                    id: data.id
                }, resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '操作成功',
                            center: true
                        });
                        this.getTableData();
                    } else {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            }).catch(() => {
            });
        }
    }
};
</script>

<style lang="less" scoped>
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
}
.operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
        margin-left: 10px;
    }
}
.content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
}
.search-wrapper {
    padding: 15px;
    background-color: #fff;
}
.filter-item {
    .el-form-item {
        margin-bottom: 0;
    }
}
.search-btn-wrapper {
    margin-top: 15px;
}
.table-wrapper {
    flex: 1;
    background-color: #fff;

    .el-button {
        padding: 0;
    }

    .btn-delete {
        color: #fd4d32;
        &.is-disabled {
            color: #c0c4cc;
            cursor: not-allowed;
        }
    }
}
.pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}
.pic-preview-desc {
    padding: 24px 24px 10px;
    border-bottom: 1px solid #e9e9e9;
    .title {
        margin-bottom: 10px;
        font-size: 16px;
        line-height: 24px;
        color: #000;

        .main-title {
            font-weight: bold;
        }

        .sub-title {
            display: inline-block;
            margin-left: 15px;
            color: rgba(0, 0, 0, .65);
        }
    }
    .remark {
        line-height: 22px;
        color: #333;
    }
}
</style>
<style lang="less">
.person-list-container {
    .filter-item {
        .el-form-item__label {
            padding: 0;
            line-height: 32px;
        }
        .el-form-item__content {
            line-height: 32px;
        }
    }
}
</style>


